#include "menu.h"
#include "lv_port.h"
#include "main_menu_page.h"
#include "keysound.h"
enum {
    LV_MENU_ITEM_BUILDER_VARIANT_1,
    LV_MENU_ITEM_BUILDER_VARIANT_2
};
typedef uint8_t lv_menu_builder_variant_t;
static lv_obj_t * menu;
static lv_obj_t * contItem1;

static void remove_group_obj(void);
static lv_obj_t * create_text(lv_obj_t * parent, const char * icon, const char * txt,
                              lv_menu_builder_variant_t builder_variant);
static lv_obj_t * create_switch(lv_obj_t * parent,
                                const char * icon, const char * txt, bool chk);
static void switch_handler(lv_event_t * e);


static lv_obj_t * create_text(lv_obj_t * parent, const char * icon, const char * txt,
                              lv_menu_builder_variant_t builder_variant)
{
    lv_obj_t * obj = lv_menu_cont_create(parent);
    lv_obj_set_style_radius(obj, 12, 0);  // 圆角

    lv_obj_t * img = NULL;
    lv_obj_t * label = NULL;
    if(icon) {
        img = lv_img_create(obj);
        lv_img_set_src(img, icon);
    }

    if(txt) {
        label = lv_label_create(obj);
        lv_label_set_text(label, txt);
        lv_label_set_long_mode(label, LV_LABEL_LONG_SCROLL_CIRCULAR);
        lv_obj_set_flex_grow(label, 1);
    }

    if(builder_variant == LV_MENU_ITEM_BUILDER_VARIANT_2 && icon && txt) {
        lv_obj_add_flag(img, LV_OBJ_FLAG_FLEX_IN_NEW_TRACK);
        lv_obj_swap(img, label);
    }

    return obj;
}

static void menu_item_event_cb(lv_event_t * e)
{
    lv_event_code_t code = lv_event_get_code(e);
    
    // lv_obj_t * menu_item_btn = lv_event_get_user_data(e);
    lv_obj_t * menu_item_btn = lv_event_get_target(e);
    switch (code)
    {
        case LV_EVENT_FOCUSED:// LV_EVENT_CLICKED
            // remove_group_obj();
            // main_menu_page_anim_out(0);
            // wifi_page(500);
            lv_obj_scroll_to_view(menu_item_btn, LV_ANIM_ON);
            break;
        case LV_EVENT_CLICKED:
            ESP_LOGI("menu", "clicked...");
            if (menu_item_btn == contItem1) {
                // playing music
                ESP_LOGE("menu", "startPlayMusic();");
                startPlayMusic();
            }
            break;
        default:
            break;
    }
}

// 隐藏滚动条的定时器回调
static void hide_scrollbar(lv_timer_t *timer) {
    lv_obj_t *obj = timer->user_data;
    lv_obj_set_scrollbar_mode(obj, LV_SCROLLBAR_MODE_OFF);
    lv_timer_del(timer);
}
// 滚动事件回调
static void on_scroll(lv_event_t *e) {
    lv_obj_t *obj = lv_event_get_target(e);
    lv_obj_set_scrollbar_mode(obj, LV_SCROLLBAR_MODE_ON);
    lv_timer_t *timer = lv_timer_create(hide_scrollbar, 500, obj);
}
static void close_mbox_event_handler(lv_event_t * e)
{
    lv_obj_t * mbox = lv_event_get_target(e);
    lv_obj_t * mbox_close_btn = lv_msgbox_get_close_btn(mbox);
    lv_group_t *group = get_group();
    lv_group_remove_obj(mbox_close_btn);
    lv_obj_del(mbox); // 删除消息框
}
static void back_event_handler(lv_event_t * e)
{
    lv_obj_t * obj = lv_event_get_target(e);
    lv_obj_t * currMenu = lv_event_get_user_data(e);

    if(lv_menu_back_btn_is_root(currMenu, obj)) {
        // lv_obj_t * mbox1 = lv_msgbox_create(NULL, "Hello", "Root back btn click.", NULL, true);
        // // 为消息框的关闭按钮添加事件处理函数
        // lv_obj_t * mbox_close_btn = lv_msgbox_get_close_btn(mbox1);
        // if (mbox_close_btn) {
        //     lv_obj_add_event_cb(mbox_close_btn, close_mbox_event_handler, LV_EVENT_CLICKED, NULL);
        // }
        // lv_obj_center(mbox1);
        
        // lv_group_t *group = get_group();
        // lv_group_add_obj(group, mbox_close_btn);
        lv_obj_del(currMenu);
        main_menu_page(0);
    }
}

// 子菜单返回键事件处理函数
static void sub_menu_back_event_handler(lv_event_t * e) {
    lv_obj_t * obj = lv_event_get_target(e);
    lv_obj_t * currMenu = lv_event_get_user_data(e);
    ESP_LOGE("menu", "sub_menu_back_event_handler, stop playing music.");
    stopPlayMusic();
}

void menuSample(void)
{
    lv_group_t *group = get_group();
    /*Create a menu object*/
    // lv_obj_t * menu = lv_menu_create(lv_scr_act());
    menu = lv_menu_create(lv_scr_act());
    lv_obj_set_size(menu, lv_disp_get_hor_res(NULL), lv_disp_get_ver_res(NULL));
    //根菜单显示返回按钮
    lv_menu_set_mode_root_back_btn(menu, LV_MENU_ROOT_BACK_BTN_ENABLED);
    // 点击根菜单返回按钮时的回调
    lv_obj_add_event_cb(menu, back_event_handler, LV_EVENT_CLICKED, menu);
    lv_obj_center(menu);
    
    lv_obj_set_style_bg_opa(menu, LV_OPA_COVER, 0);
    lv_obj_set_style_bg_color(menu, lv_color_black(), 0); // 背景颜色

    /*Modify the header*/
    lv_obj_t * back_btn = lv_menu_get_main_header_back_btn(menu);
    lv_obj_t * back_btn_label = lv_label_create(back_btn);
    lv_label_set_text(back_btn_label, "Back");
    lv_obj_add_event_cb(back_btn, sub_menu_back_event_handler, LV_EVENT_CLICKED, menu);


    lv_obj_t * cont;
    lv_obj_t * label;

    /*Create sub pages*/
    lv_obj_t * sub_1_page = lv_menu_page_create(menu, "Music");

    cont = lv_menu_cont_create(sub_1_page);
    label = lv_label_create(cont);
    lv_label_set_text(label, "Playing ...");

    // 为子菜单的返回键添加事件处理函数
    lv_obj_t * sub_2_page = lv_menu_page_create(menu, "Settings");
    cont = lv_menu_cont_create(sub_2_page);
    // label = lv_label_create(cont);
    lv_obj_t * contKeySound = create_switch(cont, LV_SYMBOL_AUDIO, "Key sound", false);
    lv_obj_t *keySoundSw = lv_obj_get_child(contKeySound, 2);
    lv_obj_add_state(keySoundSw, isKeySound() ? LV_STATE_CHECKED : 0);
    lv_obj_add_event_cb(lv_obj_get_child(contKeySound, 2), switch_handler, LV_EVENT_VALUE_CHANGED, menu);
    // lv_label_set_text(label, "Hello, I am hiding here");

    lv_obj_t * sub_3_page = lv_menu_page_create(menu, "Page 3");

    cont = lv_menu_cont_create(sub_3_page);
    label = lv_label_create(cont);
    lv_label_set_text(label, "Hello, I am hiding here");

    /*Create a main page*/
    lv_obj_t * main_page = lv_menu_page_create(menu, NULL);

    contItem1 = create_text(main_page, LV_SYMBOL_AUDIO, 
                                        "Music",
                                         LV_MENU_ITEM_BUILDER_VARIANT_1);
    // 焦点移动到菜单项，用于滚动到可视区域
    lv_obj_add_event_cb(contItem1, menu_item_event_cb, LV_EVENT_FOCUSED, NULL);  
    // 点击菜单项，执行事件处理函数
    lv_obj_add_event_cb(contItem1, menu_item_event_cb, LV_EVENT_CLICKED, menu);  
    lv_menu_set_load_page_event(menu, contItem1, sub_1_page);  // 点击菜单项， 加载子页面

    
    lv_obj_t * contItem2 = create_text(main_page, LV_SYMBOL_SETTINGS, 
                                        "Settings",
                                         LV_MENU_ITEM_BUILDER_VARIANT_1);
    lv_obj_add_event_cb(contItem2, menu_item_event_cb, LV_EVENT_FOCUSED, NULL);  // 焦点移动到菜单项，用于滚动到可视区域
    lv_menu_set_load_page_event(menu, contItem2, sub_2_page);  // 点击菜单项， 加载子页面

    lv_obj_t * contItem3 = create_text(main_page, LV_SYMBOL_VIDEO, 
                                        "Video",
                                         LV_MENU_ITEM_BUILDER_VARIANT_1);
    lv_obj_add_event_cb(contItem3, menu_item_event_cb, LV_EVENT_FOCUSED, NULL);  // 焦点移动到菜单项，用于滚动到可视区域
    lv_menu_set_load_page_event(menu, contItem3, sub_3_page);  // 点击菜单项， 加载子页面

    lv_obj_t * contItem4 = create_text(main_page, LV_SYMBOL_LIST, 
                                        "List",
                                         LV_MENU_ITEM_BUILDER_VARIANT_1);
    lv_obj_add_event_cb(contItem4, menu_item_event_cb, LV_EVENT_FOCUSED, NULL);  // 焦点移动到菜单项，用于滚动到可视区域

    lv_obj_t * contItem5 = create_text(main_page, LV_SYMBOL_POWER, 
                                        "Power",
                                         LV_MENU_ITEM_BUILDER_VARIANT_1);
    lv_obj_add_event_cb(contItem5, menu_item_event_cb, LV_EVENT_FOCUSED, NULL);  // 焦点移动到菜单项，用于滚动到可视区域

    lv_obj_t * contItem6 = create_text(main_page, LV_SYMBOL_DOWNLOAD, 
                                        "Download",
                                         LV_MENU_ITEM_BUILDER_VARIANT_1);
    lv_obj_add_event_cb(contItem6, menu_item_event_cb, LV_EVENT_FOCUSED, NULL);  // 焦点移动到菜单项，用于滚动到可视区域

    lv_obj_t * contItem7 = create_text(main_page, LV_SYMBOL_DRIVE, 
                                        "Drive",
                                         LV_MENU_ITEM_BUILDER_VARIANT_1);
    lv_obj_add_event_cb(contItem7, menu_item_event_cb, LV_EVENT_FOCUSED, NULL);  // 焦点移动到菜单项，用于滚动到可视区域

    lv_obj_t * contItem8 = create_text(main_page, LV_SYMBOL_REFRESH, 
                                        "Refresh",
                                         LV_MENU_ITEM_BUILDER_VARIANT_1);
    lv_obj_add_event_cb(contItem8, menu_item_event_cb, LV_EVENT_FOCUSED, NULL);  // 焦点移动到菜单项，用于滚动到可视区域

    lv_obj_t * contItem9 = create_text(main_page, LV_SYMBOL_MUTE, 
                                        "Mute",
                                         LV_MENU_ITEM_BUILDER_VARIANT_1);
    lv_obj_add_event_cb(contItem9, menu_item_event_cb, LV_EVENT_FOCUSED, NULL);  // 焦点移动到菜单项，用于滚动到可视区域

    lv_obj_t * contItem10 = create_text(main_page, LV_SYMBOL_VOLUME_MID, 
                                        "Mid",
                                         LV_MENU_ITEM_BUILDER_VARIANT_1);
    lv_obj_add_event_cb(contItem10, menu_item_event_cb, LV_EVENT_FOCUSED, NULL);  // 焦点移动到菜单项，用于滚动到可视区域

    lv_obj_t * contItem11 = create_text(main_page, LV_SYMBOL_VOLUME_MAX, 
                                        "Max",
                                         LV_MENU_ITEM_BUILDER_VARIANT_1);
    lv_obj_add_event_cb(contItem11, menu_item_event_cb, LV_EVENT_FOCUSED, NULL);  // 焦点移动到菜单项，用于滚动到可视区域

    lv_obj_t * contItem12 = create_text(main_page, LV_SYMBOL_IMAGE, 
                                        "Image",
                                         LV_MENU_ITEM_BUILDER_VARIANT_1);
    lv_obj_add_event_cb(contItem12, menu_item_event_cb, LV_EVENT_FOCUSED, NULL);  // 焦点移动到菜单项，用于滚动到可视区域

    lv_obj_t * contItem13 = create_text(main_page, LV_SYMBOL_TINT, 
                                        "Tini",
                                         LV_MENU_ITEM_BUILDER_VARIANT_1);
    lv_obj_add_event_cb(contItem13, menu_item_event_cb, LV_EVENT_FOCUSED, NULL);  // 焦点移动到菜单项，用于滚动到可视区域

    lv_obj_t * contItem14 = create_text(main_page, LV_SYMBOL_PREV, 
                                        "Prev",
                                         LV_MENU_ITEM_BUILDER_VARIANT_1);
    lv_obj_add_event_cb(contItem14, menu_item_event_cb, LV_EVENT_FOCUSED, NULL);  // 焦点移动到菜单项，用于滚动到可视区域

    lv_obj_t * contItem15 = create_text(main_page, LV_SYMBOL_PLAY, 
                                        "Play",
                                         LV_MENU_ITEM_BUILDER_VARIANT_1);
    lv_obj_add_event_cb(contItem15, menu_item_event_cb, LV_EVENT_FOCUSED, NULL);  // 焦点移动到菜单项，用于滚动到可视区域

    lv_obj_t * contItem16 = create_text(main_page, LV_SYMBOL_PAUSE, 
                                        "Pause",
                                         LV_MENU_ITEM_BUILDER_VARIANT_1);
    lv_obj_add_event_cb(contItem16, menu_item_event_cb, LV_EVENT_FOCUSED, NULL);  // 焦点移动到菜单项，用于滚动到可视区域

    lv_obj_t * contItem17 = create_text(main_page, LV_SYMBOL_STOP, 
                                        "Stop",
                                         LV_MENU_ITEM_BUILDER_VARIANT_1);
    lv_obj_add_event_cb(contItem17, menu_item_event_cb, LV_EVENT_FOCUSED, NULL);  // 焦点移动到菜单项，用于滚动到可视区域

    lv_obj_t * contItem18 = create_text(main_page, LV_SYMBOL_NEXT, 
                                        "Stop",
                                         LV_MENU_ITEM_BUILDER_VARIANT_1);
    lv_obj_add_event_cb(contItem18, menu_item_event_cb, LV_EVENT_FOCUSED, NULL);  // 焦点移动到菜单项，用于滚动到可视区域

    lv_obj_t * contItem19 = create_text(main_page, LV_SYMBOL_EJECT, 
                                        "Eject",
                                         LV_MENU_ITEM_BUILDER_VARIANT_1);
    lv_obj_add_event_cb(contItem19, menu_item_event_cb, LV_EVENT_FOCUSED, NULL);  // 焦点移动到菜单项，用于滚动到可视区域

    lv_obj_t * contItem20 = create_text(main_page, LV_SYMBOL_LEFT, 
                                        "Left",
                                         LV_MENU_ITEM_BUILDER_VARIANT_1);
    lv_obj_add_event_cb(contItem20, menu_item_event_cb, LV_EVENT_FOCUSED, NULL);  // 焦点移动到菜单项，用于滚动到可视区域

    lv_obj_t * contItem21 = create_text(main_page, LV_SYMBOL_RIGHT, 
                                        "Right",
                                         LV_MENU_ITEM_BUILDER_VARIANT_1);
    lv_obj_add_event_cb(contItem21, menu_item_event_cb, LV_EVENT_FOCUSED, NULL);  // 焦点移动到菜单项，用于滚动到可视区域

    lv_obj_t * contItem22 = create_text(main_page, LV_SYMBOL_DIRECTORY, 
                                        "Directory",
                                         LV_MENU_ITEM_BUILDER_VARIANT_1);
    lv_obj_add_event_cb(contItem22, menu_item_event_cb, LV_EVENT_FOCUSED, NULL);  // 焦点移动到菜单项，用于滚动到可视区域

   
    lv_menu_set_page(menu, main_page);
    
    lv_group_add_obj(group, contItem1);
    lv_group_add_obj(group, contItem2);
    lv_group_add_obj(group, contItem3);
    lv_group_add_obj(group, contItem4);
    lv_group_add_obj(group, contItem5);
    lv_group_add_obj(group, contItem6);
    lv_group_add_obj(group, contItem7);
    lv_group_add_obj(group, contItem8);
    lv_group_add_obj(group, contItem9);
    lv_group_add_obj(group, contItem10);
    lv_group_add_obj(group, contItem11);
    lv_group_add_obj(group, contItem12);
    lv_group_add_obj(group, contItem13);
    lv_group_add_obj(group, contItem14);
    lv_group_add_obj(group, contItem15);
    lv_group_add_obj(group, contItem16);
    lv_group_add_obj(group, contItem17);
    lv_group_add_obj(group, contItem18);
    lv_group_add_obj(group, contItem19);
    lv_group_add_obj(group, contItem20);
    lv_group_add_obj(group, contItem21);
    lv_group_add_obj(group, contItem22);
    lv_group_add_obj(group, back_btn);  //back_btn

    // 设置滚动条模式为 OFF
    // lv_obj_set_style_scrollbar_mode(menu, LV_SCROLLBAR_MODE_OFF, 0);
    // 添加滚动事件回调
    lv_obj_add_event_cb(menu, on_scroll, LV_EVENT_SCROLL, NULL);
    lv_obj_set_scrollbar_mode(menu, LV_SCROLLBAR_MODE_OFF);
}

static lv_obj_t * create_switch(lv_obj_t * parent, const char * icon, const char * txt, bool chk)
{
    lv_obj_t * obj = create_text(parent, icon, txt, LV_MENU_ITEM_BUILDER_VARIANT_1);

    lv_obj_t * sw = lv_switch_create(obj);
    lv_group_t *group = get_group();
    lv_group_add_obj(group, sw);  //back_btn
    
    lv_obj_add_state(sw, chk ? LV_STATE_CHECKED : 0);

    return obj;
}


static void switch_handler(lv_event_t * e)
{
    lv_event_code_t code = lv_event_get_code(e);
    lv_obj_t * menu = lv_event_get_user_data(e);
    lv_obj_t * obj = lv_event_get_target(e);
    if(code == LV_EVENT_VALUE_CHANGED) {
        if(lv_obj_has_state(obj, LV_STATE_CHECKED)) {
            setKeySound(true);
        } else {
            setKeySound(false);
        }
    }
}